package com.briup.jd2407cms.web.controller;

import com.briup.jd2407cms.aop.Logging;
import com.briup.jd2407cms.domain.User;
import com.briup.jd2407cms.service.UserService;
import com.briup.jd2407cms.util.result.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @BelongsProject: JD2407-cms
 * @BelongsPackage: com.briup.jd2407cms.web.controller
 * @Author: 鲨鲨天下第一
 * @CreateTime: 2024-11-19  12:00
 * @Description: TODO
 * @Version: 1.0
 */
@Api(tags = "用户模块")
@RestController
@Slf4j
@RequestMapping("/auth/user")
public class UserController {
    @Autowired
    private UserService userService;

    //admin使用：用户登录后首页展示个人信息需要使用
    @ApiOperation("获取用户个人信息")
    @Logging("获取用户个人信息")
    @GetMapping("/info")  //@RequestAttribute 请参考JwtInterceptor中 38行代码
    public Result getInfo(@RequestAttribute("userId") String id) {
        return Result.success(userService.getById(id));
    }

    @ApiOperation(value = "新增用户", notes = "username、password必须存在不为空，且username唯一")
    @Logging("新增用户")
    @PostMapping("/save")
    public Result save(@RequestBody User user){
        return Result.success(userService.save(user)?"操作成功":"操作失败");
    }

//    @JsonSerialize(using = ToStringSerializer.class)

    @ApiOperation(value = "根据id查找用户", notes = "id必须存在且有效")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "用户id", required = true, paramType = "path", dataType = "String")
    })
    @Logging("根据id查找用户")
    @GetMapping("/queryById/{id}")
    public Result queryById(@PathVariable Long id) {
        return Result.success(userService.getById(id));
    }

    @ApiOperation(value = "分页+条件查询用户", notes = "用户中要含角色信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "页码", required = true, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "每页条数", required = true, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "username", value = "用户名", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "status", value = "状态：启用|禁用", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "roleId", value = "角色id", dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "isVip", value = "是否为会员: 0|1", dataType = "int", paramType = "query")
    })
    @Logging("分页+条件查询用户")
    @GetMapping("/query")
    public Result query(@RequestParam Integer pageNum, @RequestParam Integer pageSize,
                        String username, String status, Integer roleId, Integer isVip) {
        return Result.success(userService.query(pageNum, pageSize, username, status, roleId, isVip));
    }

    @ApiOperation(value = "设置用户为Vip", notes = "id存在且有效")
    @Logging("设置用户为Vip")
    @PutMapping("/setVip/{id}")
    public Result setVip(@PathVariable Long id) {
//        userService.setVip(id);
        return Result.success("更新成功");
    }

    @ApiOperation(value = "更新用户信息", notes = "id必须存在且有效，如果username存在则必须唯一")
    @Logging("更新用户信息")
    @PutMapping("/update")
    public Result update(@RequestBody User user) {
        return Result.success(userService.updateById(user)?"操作成功":"操作失败");
    }

    @ApiOperation(value = "根据id删除用户", notes = "id必须存在且有效")
    @Logging("根据id删除用户")
    @DeleteMapping("/deleteByBatch/{ids}")
    public Result removeByBatch(@PathVariable List<String> ids){
        return Result.success(userService.removeByIds(ids)?"操作成功":"操作失败");
    }

    @ApiOperation(value = "查询资讯模块的发布者", notes = "用于展示资讯模块的发布者")
    @Logging("查询资讯模块的发布者")
    @GetMapping("/getAllUser")
    public Result getAllUser(){
        return Result.success(userService.list());
    }


}
